﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SocialShare.DAL.Facades;
using SocialShare.BusinessEntities;
using SocialShare.BLL;
using System.Configuration;
using SocialShare.Web.Integrations;
using Utilities;

namespace SocialShare.Web.My_Account
{
    public partial class new_blog_post : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void lnbAddNew_Click(object sender, EventArgs e)
        {
            ConfigReader configReader = new ConfigReader();
            if (String.IsNullOrEmpty(Server.HtmlDecode(hidCKEDitorValue.Value.Trim())))
            {
                divMessage.Visible = true;
                divMessage.Attributes.Add("class", "error");
                lblMessage.Text = configReader.ReadConfigurationValue(ConfigurationManager.AppSettings[APP_SETTINGS_NAME.MessageLocation.ToString()], SITE_MESSAGES.EMPTY_MESSAGE.ToString());
                return;
            }
           
            StrategyProxyUser currentLoggedInUser = new StrategyProxyUser();

            BlogFacade blogFacade = new BlogFacade();
            SocialShare_Blogs blog = new SocialShare_Blogs();
            blog.DateCreated = DateTime.Now;
            blog.PostContent = Server.HtmlEncode(hidCKEDitorValue.Value.Trim());
            blog.PostSummary = Server.HtmlEncode(textareaSummary.Value.Trim());
            blog.PostTitle = txtTitle.Text.Trim();
            blog.IsCommentsAllowed = chkAllowComments.Checked;
            blog.UserId = currentLoggedInUser.GetCurrentUser().User_ID;
            blog.PostStatus = (Int32)BlogStatus.Published;
            Int32 postId = blogFacade.SaveBlog(blog);

            if (postId > 0)
            {
                divMessage.Visible = true;
                divMessage.Attributes.Add("class", "success");
                lblMessage.Text = configReader.ReadConfigurationValue(ConfigurationManager.AppSettings[APP_SETTINGS_NAME.MessageLocation.ToString()], SITE_MESSAGES.BLOG_POST_SAVE_SUCCESS.ToString());
                ClearText();
            }
            else
            {
                divMessage.Visible = true;
                divMessage.Attributes.Add("class", "error");
                lblMessage.Text = configReader.ReadConfigurationValue(ConfigurationManager.AppSettings[APP_SETTINGS_NAME.MessageLocation.ToString()], SITE_MESSAGES.FAILED_MESSAGE_POST.ToString());
            }
        }

        private void ClearText()
        {
            txtTitle.Text = String.Empty;
            //textareaSummary.Value = String.Empty;
            //ckeditor.Value = String.Empty;
        }
    }

    
}